<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="考核类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectId">
              <j-dict-select-tag type="list" v-model="model.projectId" dictCode="ASSESSMENT_PROJECTS" placeholder="请选择考核类型" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="考核大类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classificationId">
              <a-select v-model="model.classificationId" >
                <a-select-option v-for="classification in this.classificationData" :key="classification.id">
                  {{ classification.name }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="考核项" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="itemsValue">
              <a-select v-model="model.itemsValue" >
                <a-select-option v-for="item in this.itemsData" :key="item.item_value" placeholder="请输入考核项">
                  {{ item.item_text }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'AssessmentProjectsItemsForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
      projectId: {
        type:String,
        required:true
      },
      classificationId:{
        type:String,
        required:true
      }
    },
    data () {
      return {
        model:{
          projectId: this.projectId,
          classificationId:this.classificationId,
          itemsValue:''
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/manage/assessmentProjectsItems/add",
          edit: "/manage/assessmentProjectsItems/edit",
          queryById: "/manage/assessmentProjectsItems/queryById",
          queryClassificatioByProjectID : '/assessmentProjectsClassification/list',
          querySurplusItemByProjectId:'/manage/assessmentProjectsItems/querySurplusItemByProjectId'
        },
        classificationData:[],
        itemsData:[]
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.initClassification();
      this.initItems();
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);

        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      initClassification(){
        this.confirmLoading = true;
        getAction(this.url.queryClassificatioByProjectID, { projectId: this.projectId} ).then((res) => {
            if (res.success) {
              let result = res.result.records||res.result;
              console.log(result)


              this.classificationData =result
            }
            if(res.code===510){
              this.$message.warning(res.message)
            }

          }).finally(() => {
            this.confirmLoading = false;
          });
      },
      initItems(){
        this.confirmLoading = true;
        getAction(this.url.querySurplusItemByProjectId, { projectId: this.projectId} ).then((res) => {
          if (res.success) {
            let result =  res.result.records||res.result;
            console.log(result)
            if(this.model.itemText != undefined){
              result.push({item_value:this.model.itemsValue, item_text:this.model.itemText})
            }
            console.log(result)
            this.itemsData = result;
          }
          if(res.code===510){
            this.$message.warning(res.message)
          }
          console.log(this.itemsData)
        }).finally(() => {
          this.confirmLoading = false;
        });
      }
    }
  }
</script>